package com.manager
{
	import com.engine.AwayEngine;
	
	import away3d.materials.ColorMaterial;
	import away3d.materials.methods.EffectMethodBase;
	import away3d.materials.methods.EnvMapMethod;
	import away3d.textures.BitmapCubeTexture;
	import away3d.utils.Cast;

	public class CarMaterial
	{
		[Embed(source="garage/EnvPosXGarage.jpg")]
		private static var EnvPosXGarage:Class;
		[Embed(source="garage/EnvPosYGarage.jpg")]
		private static var EnvPosYGarage:Class;
		[Embed(source="garage/EnvPosZGarage.jpg")]
		private static var EnvPosZGarage:Class;
		[Embed(source="garage/EnvNegXGarage.jpg")]
		private static var EnvNegXGarage:Class;
		[Embed(source="garage/EnvNegYGarage.jpg")]
		private static var EnvNegYGarage:Class;
		[Embed(source="garage/EnvNegZGarage.jpg")]
		private static var EnvNegZGarage:Class;
		
		private static var cubeTextures:BitmapCubeTexture;
		
		private static var _bodyReflection:EnvMapMethod;
		private static var _glassReflection:EnvMapMethod;
		private static var _rimsReflection:EnvMapMethod;
		
		private static var _bodyMaterial:ColorMaterial;

		public static function get rimsReflection():EnvMapMethod
		{
			return _rimsReflection;
		}

		public static function get glassReflection():EnvMapMethod
		{
			return _glassReflection;
		}

		public static function get bodyReflection():EnvMapMethod
		{
			return _bodyReflection;
		}

		/** 车身 */
		public static function get bodyMaterial():ColorMaterial
		{
			return _bodyMaterial;
		}
		private static var _glassMaterial:ColorMaterial;
		/** 车玻璃 */
		public static function get glassMaterial():ColorMaterial
		{
			return _glassMaterial;
		}
		private static var _bumperMaterial:ColorMaterial;
		/** 车壳*/
		public static function get bumperMaterial():ColorMaterial
		{
			return _bumperMaterial;
		}
		private static var _rimsMaterial:ColorMaterial;
		/** 车轮*/
		public static function get rimsMaterial():ColorMaterial
		{
			return _rimsMaterial;
		}
		
		public static function changeColor(bodyColor:uint = 0x242424,
										   bumperColor:uint = 0x444444):void{
			if(bodyReflection == null){
				createReflection();
			}
			if(_bodyMaterial == null){
				createMaterial();
			}
			_bodyMaterial.color = bodyColor;
			_bumperMaterial.color = bumperColor;
		}
		/**
		 *获取环境反射映像 
		 * @param value 传入透明度
		 * @return 返回环境反射映像
		 */		
		public static function getReflection(value:Number):EffectMethodBase
		{
			if(cubeTextures == null) creaetBox();
			return new EnvMapMethod(cubeTextures,value);
			return null;
		}
		
		public static function weelColor(bodyColor:uint = 0x242424):void
		{
			_rimsMaterial.color = bodyColor;
		}
		
		private static function createMaterial():void
		{
			_bodyMaterial = new ColorMaterial(0x242424);
			_bodyMaterial.specular = 1;
			_bodyMaterial.lightPicker = AwayEngine.lightPicker;
			_bodyMaterial.addMethod(bodyReflection);
			//玻璃贴图
			_glassMaterial = new ColorMaterial(0xFFFFFF, 0.4);
			_glassMaterial.specular = 1;
			_glassMaterial.lightPicker = AwayEngine.lightPicker;
			_glassMaterial.addMethod(glassReflection);
			//车轮壳贴图
			_rimsMaterial = new ColorMaterial(0xEEEEEE);
			_rimsMaterial.specular = 0.5;
			_rimsMaterial.lightPicker = AwayEngine.lightPicker;
			_rimsMaterial.addMethod(rimsReflection);
			//车壳贴图
			_bumperMaterial = new ColorMaterial(0x444444);
			_bumperMaterial.specular = 0.1;
			_bumperMaterial.lightPicker = AwayEngine.lightPicker;
			_bumperMaterial.addMethod(rimsReflection);
		}
		
		private static function creaetBox():void
		{
			cubeTextures = 
				new BitmapCubeTexture(Cast.bitmapData(EnvPosXGarage), 
					Cast.bitmapData(EnvNegXGarage), 
					Cast.bitmapData(EnvPosYGarage), 
					Cast.bitmapData(EnvNegYGarage), 
					Cast.bitmapData(EnvPosZGarage), 
					Cast.bitmapData(EnvNegZGarage));
		}
		
		//环境贴图模式,让车身可以有镜面效果
		private static function createReflection():void{
			creaetBox();
			_bodyReflection = new EnvMapMethod(cubeTextures, 0.3);
			_glassReflection = new EnvMapMethod(cubeTextures);
			_rimsReflection = new EnvMapMethod(cubeTextures, 0.5);
		}
		
	}
}